package com.cat.model.web.action.api;

import java.math.BigDecimal;
import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.cat.model.biz.service.api.AccountService;
import com.cat.model.common.model.common.AcntWithdrawVO;
import com.cat.model.common.model.common.UserTokenVO;
import com.cat.model.common.util.ErrorCode;
import com.cat.model.dao.mapper.UserInfoMapper;
import com.cat.model.web.action.base.BaseAction;

public class AccountAction extends BaseAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = 604080577375901309L;
	
	private String token;
	private Long accountId;
	private BigDecimal amount;
	private String bankAcntNo;
	private String bankAcntName;
	private Integer page;
	private Integer size;
	
	@Autowired
	private UserInfoMapper userInfoMapper;
	
	@Autowired
	private AccountService accountService;
	
	/**
	 * 获取账户信息
	 * */
	public void info(){
		if(StringUtils.isEmpty(token)){
			print(ErrorCode.PARAM_ERROR.response());
			return;
		}
		//token过期
		UserTokenVO userToken = userInfoMapper.getUserTokenByToken(token);
		if(null == userToken || userToken.getTokenExpire() < new Date().getTime()){
			print(ErrorCode.TOKEN_EXPIRE.response());
			return;
		}
		
		print(accountService.accountInfo(userToken.getUserId()));
	}
	
	/**
	 * 账户交易明细
	 * */
	public void detailList(){
		if(StringUtils.isEmpty(token)||accountId==null||page==null||size==null){
			print(ErrorCode.PARAM_ERROR.response());
			return;
		}
		//token过期
		UserTokenVO userToken = userInfoMapper.getUserTokenByToken(token);
		if(null == userToken || userToken.getTokenExpire() < new Date().getTime()){
			print(ErrorCode.TOKEN_EXPIRE.response());
			return;
		}
		
		print(accountService.getAccountDList(userToken.getUserId(),accountId, page, size));
	}
	
	/**
	 * 账户提现申请
	 * */
	public void withdrawApply(){
		if(StringUtils.isEmpty(token)||amount==null||StringUtils.isEmpty(bankAcntNo)||StringUtils.isEmpty(bankAcntName)){
			print(ErrorCode.PARAM_ERROR.response());
			return;
		}
		//token过期
		UserTokenVO userToken = userInfoMapper.getUserTokenByToken(token);
		if(null == userToken || userToken.getTokenExpire() < new Date().getTime()){
			print(ErrorCode.TOKEN_EXPIRE.response());
			return;
		}
		
		AcntWithdrawVO withdraw = new AcntWithdrawVO();
		withdraw.setAmount(amount);
		withdraw.setBankAcntNo(bankAcntNo);
		withdraw.setBankAcntName(bankAcntName);
		
		print(accountService.withdrawApply(userToken.getUserId(), withdraw));
	}
	
	/**
	 * 账户提现记录
	 * */
	public void withdrawList(){
		if(StringUtils.isEmpty(token)||page==null||size==null){
			print(ErrorCode.PARAM_ERROR.response());
			return;
		}
		//token过期
		UserTokenVO userToken = userInfoMapper.getUserTokenByToken(token);
		if(null == userToken || userToken.getTokenExpire() < new Date().getTime()){
			print(ErrorCode.TOKEN_EXPIRE.response());
			return;
		}
		
		print(accountService.getWithdrawList(userToken.getUserId(),page, size));
	}

	public String getToken() {
		return token;
	}

	public void setToken(String token) {
		this.token = token;
	}

	public Integer getPage() {
		return page;
	}

	public void setPage(Integer page) {
		this.page = page;
	}

	public Integer getSize() {
		return size;
	}

	public void setSize(Integer size) {
		this.size = size;
	}

	public Long getAccountId() {
		return accountId;
	}

	public void setAccountId(Long accountId) {
		this.accountId = accountId;
	}

	public BigDecimal getAmount() {
		return amount;
	}

	public void setAmount(BigDecimal amount) {
		this.amount = amount;
	}

	public String getBankAcntNo() {
		return bankAcntNo;
	}

	public void setBankAcntNo(String bankAcntNo) {
		this.bankAcntNo = bankAcntNo;
	}

	public String getBankAcntName() {
		return bankAcntName;
	}

	public void setBankAcntName(String bankAcntName) {
		this.bankAcntName = bankAcntName;
	}

}
